package com.amesit.service.privilege.impl;

import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.amesit.service.base.DaoSupport;
import com.amesit.service.privilege.EmployeeService;

@Service @Transactional
public class EmployeeServiceBean extends DaoSupport implements EmployeeService {

	@Transactional(readOnly=true, propagation=Propagation.NOT_SUPPORTED)
	public boolean exist(String username) {
		Long count = (Long)em.createQuery("select count(o) from Employee o where o.username=?1").setParameter(1, username).getSingleResult();
		return count>0;
	}

	@Override
	public <T> void delete(Class<T> entityClass, Object[] entityids) {
		if(entityids!=null && entityids.length>0){
			StringBuffer jpql = new StringBuffer("");
			for(int i=0;i<entityids.length; i++){
				jpql.append('?').append(i+2).append(',');
			}
			jpql.deleteCharAt(jpql.length()-1);
			Query query = em.createQuery("update Employee o set o.visible=?1 where o.username in("+ jpql.toString()+")")
				.setParameter(1, false);
			for(int i=0;i<entityids.length; i++){
				query.setParameter(i+2, entityids[i]);
			}
			query.executeUpdate();
		}		
	}
}
